/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.spring.web.servlet.view.JsonView;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Listing;
import org.cx1.model.User;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IFeaturedService;
import org.cx1.service.api.IInquiryService;
import org.cx1.service.api.IListingService;
import org.cx1.service.api.IUserService;
import org.cx1.service.impl.DFeaturedService;
import org.cx1.service.impl.DListingService;
import org.cx1.service.impl.DUserService;
import org.cx1.service.impl.MInquiryService;
import org.cx1.service.impl.MListingService;
import org.cx1.web.controller.action.FeaturedAction;
import org.cx1.web.controller.action.InquiryAction;
import org.cx1.web.controller.action.ListingAction;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author ECH
 */
public class FeaturedControllerWS extends BaseController {
    
    private IListingService listingService = new DListingService();
    private IFeaturedService featuredService = new DFeaturedService();
    
    public FeaturedControllerWS() {
        
    }
    
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
                                                 HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();        
        
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        if (ServletFileUpload.isMultipartContent(request)) {
            parameterMap = this.getMultipartFormParams(request);
        } 
        else {
            parameterMap = this.getFormParams(request);
        }
        
        Map<String, String> formfields = (Map<String, String>) parameterMap.get("formfields");
        int action = StringUtils.isEmpty(formfields.get("a")) ? 0 : Integer.parseInt(formfields.get("a"));
        
        switch (action) {         
            case FeaturedAction.VALIDATE_FEATURED_LISTING: {
                validateFeaturedListing(mav, map, request);
                break;
            }
            default: {
                break;
            }
        }          
                
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        
        mav.setView(new JsonView());
        mav.addObject("model", map);
        return mav;
    }    
    
    private void validateFeaturedListing(ModelAndView mav, Map map, HttpServletRequest request) {
        boolean success = false;
        String message = "";
        String listingID = request.getParameter("listingID");
        String city = request.getParameter("city");
        String term = request.getParameter("term");
        String strTerm = "";
        
        if (StringUtils.equalsIgnoreCase("1", term)) {
            strTerm = "long term";
        }
        else if (StringUtils.equalsIgnoreCase("2", term)) {
            strTerm = "short term";
        }
        
        boolean existing = this.featuredService.checkExisting(listingID, city, term).getResponse();
        boolean maxReached = this.featuredService.checkMaxReached(city, term).getResponse();
        boolean valid = this.featuredService.checkListingValid(listingID).getResponse();
        
        if (existing) {
            message = "This listing is already featured in " + city + " under " + strTerm + " category";
        }
        if (maxReached) {
            message = "The maximum allowed featured properties in " + city + " under " + strTerm + " category is already reached. If you want us to notify you when there is free spot on this category, email us on team@rentpad.me =)";
        }
        if (!valid) {
            message = "The property you are trying to feature is either not verified or is penalized. Please contact our support via team@rentpad.me";
        }
        
        if (StringUtils.isEmpty(message)) {
            success = true;
        }
        
        map.put("success", success);
        map.put("message", message);
    }
    
}